#include "drv_led.h"
#include "drv_beep.h"
#include "drv_key.h"
#include "drv_usart.h"
#include "string.h"
#include "drv_time.h"
#include "oled.h"
#include "bmp.h"
#include "drv_adc.h"
#include "drv_dma.h"
#include "drv_dht11.h"


uint8_t key_flag1 = 1;
uint8_t key_flag2 = 2;
uint8_t led_flag  = 3;
uint8_t key_flag4 = 4;

uint8_t page = 1;
uint8_t page_flash = 0;

uint8_t wifi_flag = 1;

uint32_t led_pwm = 0;

extern uint16_t adc_data[100];

extern char res2[100];


void wifi_check(void)
{
	usart_sendstr(USART2,"AT+RESTORE\r\n",strlen("AT+RESTORE\r\n"));
	drv_systick_delayms(2000);
	
	usart_sendstr(USART2,"AT\r\n",strlen("AT\r\n"));
	drv_systick_delayms(500);
	
	//设置客户端
	usart_sendstr(USART2,"AT+CWMODE=1\r\n",strlen("AT+CWMODE=1\r\n"));
	drv_systick_delayms(500);
	
	//设置通道
	usart_sendstr(USART2,"AT+CIPMUX=0\r\n",strlen("AT+CIPMUX=0\r\n"));
	drv_systick_delayms(500);
	
	//连接AP
	usart_sendstr(USART2,"AT+CWJAP=\"ouc\",\"ou11223344\"\r\n",strlen("AT+CWJAP=\"ouc\",\"ou11223344\"\r\n"));
	drv_systick_delayms(7000);
	
	//建立tcp连接
	usart_sendstr(USART2,"AT+CIPSTART=\"TCP\",\"192.168.43.1\",8080\r\n",strlen("AT+CIPSTART=\"TCP\",\"192.168.81.1\",8080\r\n"));
	drv_systick_delayms(2000);
}

int main()
{
	uint16_t lux;
	u8 temp;
	u8 humi;
	
	drv_systick_init(168);
	drv_beep_init();
	drv_led_init();
	drv_key_init(GPIOE, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6);
	drv_key_init2(GPIOC,GPIO_Pin_13);
	drv_usart2_init();
	drv_usart1_init();
	drv_adc_init();
	DHT11_Init();
	OLED_Init();
	drv_time1_init();
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

	page_flash = 1;
	while(1)
	{
		if(get_key_value(GPIOE,GPIO_Pin_6) == 1)
		{
			if(page > 1)
			{
				page--;
				page_flash = 1;
			}
		}
		if(get_key_value(GPIOC,GPIO_Pin_13) == 1)
		{
			if(page < 4)
			{
				page++;
 				page_flash = 1;
			}
		}
		
		if(page_flash == 1)
		{
			lux = drv_adc_res_value();
			OLED_Clear();
		}

		OLED_ShowChinese(16,0,0,16,1);
		OLED_ShowChinese(32,0,1,16,1);
		OLED_ShowChinese(48,0,2,16,1);
		OLED_ShowChinese(64,0,3,16,1);
		OLED_ShowChinese(80,0,4,16,1);
		OLED_ShowChinese(96,0,5,16,1);
		
		if(page == 1)
		{			
			OLED_ShowString(4,16,(u8*)"HUAQINGYUANJIAN",16,1);
			OLED_ShowString(20,32,(u8*)"2024/03/11",16,1);
			OLED_ShowString(100,48,(u8*)"1/4",16,1);
		}

		else if(page == 2)
		{
			drv_time1_init();
			if(lux-50 > drv_adc_res_value() || drv_adc_res_value()>lux+50)
			{
				OLED_Clear();				
			}

			if(get_key_value(GPIOE, GPIO_Pin_4) == 1)
			{
				key_flag1 = ~key_flag1;
				led_flag = ~led_flag;
				page_flash = 1;
			}
			if(get_key_value(GPIOE, GPIO_Pin_5) == 1)
			{
				key_flag2 = ~key_flag2;	
				page_flash = 1;
			}
			
			if(key_flag1 == 1)
			{					
				drv_led_close(GPIO_Pin_8);	
				OLED_ShowString(0,18,(u8*)"LED:CLOSE",16,1);
			}
			else
			{
				drv_led_init();
				drv_led_open(GPIO_Pin_8);
				OLED_ShowString(0,18,(u8*)"LED:OPEN",16,0);
			}
			if(key_flag2 == 2)
			{
				drv_beep_close();
				OLED_ShowString(0,34,(u8*)"BEEP:CLOSE",16,1);
			}
			else
			{
				drv_beep_open();
				OLED_ShowString(0,34,(u8*)"BEEP:OPEN",16,0);
		 	}
			
			if(lux> 3000)
			{		
				if(led_flag == 3)
				{
					led_pwm = 500;
					OLED_ShowString(0,18,(u8*)"LED:OPEN",16,0);
				}	
				OLED_ShowString(0, 48, "LUX:WEAK", 16, 1);
			}
			else if(2000< lux  && lux < 3000)
			{
				if(led_flag == 3)
				{
					led_pwm = 200;
					OLED_ShowString(0,18,(u8*)"LED:OPEN",16,0);
				}
				OLED_ShowString(0, 48, "LUX: MID", 16, 1);
			}
			else if(2000 > lux && lux > 1500)
			{
				if(led_flag == 3)
				{
					led_pwm = 50;
					OLED_ShowString(0,18,(u8*)"LED:OPEN",16,0);
				}
				OLED_ShowString(0, 48, "LUX: MID", 16, 1);
			}
			else if(lux < 1500)
			{
				OLED_ShowString(0, 48, "LUX:HIGH", 16, 1);
				if(led_flag == 3)
				{
					led_pwm = 0;
					drv_led_close(GPIO_Pin_8);
					OLED_ShowString(0,18,(u8*)"LED:CLOSE",16,1);
				}			
			}
			TIM_SetCompare1(TIM1,led_pwm);
			OLED_ShowNum(80,48,drv_adc_res_value(),4,16,1);
			OLED_ShowString(100,32,(u8*)"2/4",16,1);

			drv_systick_delayms(10);
			if(lux-50 > drv_adc_res_value() || drv_adc_res_value()>lux+50)
			{
				OLED_ShowChinese(16,0,0,16,1);
				OLED_ShowChinese(32,0,1,16,1);
				OLED_ShowChinese(48,0,2,16,1);
				OLED_ShowChinese(64,0,3,16,1);
				OLED_ShowChinese(80,0,4,16,1);
				OLED_ShowChinese(96,0,5,16,1);
				OLED_Refresh();
				lux = drv_adc_res_value();
			}
		}
		//显示温湿度数值
		else if(page == 3)
		{
			DHT11_Read_Data(&temp,&humi);
			OLED_ShowString(0, 16, "TEMP:", 16, 1);
			OLED_ShowNum(50, 16, temp,3,16, 1);
			
			OLED_ShowString(0, 32, "HUMI:", 16, 1);
			OLED_ShowNum(50, 32, humi,3,16,1);
	
			
			OLED_ShowString(100,48,(u8*)"3/4",16,1);
			OLED_Refresh();
		}
		//Wifi控制界面
		else if(page == 4)
		{			
			if(wifi_flag == 1)
			{
				OLED_ShowString(0,18,"wifi connectting",16,1);
				OLED_ShowString(100,48,(u8*)"4/4",16,1);
				OLED_Refresh();
				
				wifi_check();
				
				OLED_ShowString(16,32,"Successful",16,1);
				OLED_Refresh();
				drv_systick_delayms(500);
				OLED_Clear();
				wifi_flag = 0;
				
			}
			else
			{				
				
				OLED_ShowString(16,0,"  ",16,1);
				OLED_ShowChinese(32,0,6,16,1);//远
				OLED_ShowChinese(48,0,7,16,1);//程
				OLED_ShowString(64,0,"  ",16,1);
				OLED_ShowChinese(80,0,8,16,1);//控
				OLED_ShowChinese(96,0,9,16,1);//制
				OLED_Refresh();
				
				if(key_flag1 == 1)
				{
					drv_led_close(GPIO_Pin_8);
					drv_led_close(GPIO_Pin_10);
				}
				else
				{
					drv_led_open(GPIO_Pin_8);
					drv_led_open(GPIO_Pin_10);
				}
				if(key_flag2 == 2)
				{
					drv_beep_close();
				}
				else
				{
					drv_beep_open();
				}
			}		
			OLED_ShowString(100 ,48,(u8*)"4/4",16,1);
		}

		if(page_flash == 1 || page == 3)
		{
			OLED_Refresh();
			page_flash = 0;
		}

	}

}

